/*
 * @Author: scl
 * @Date: 2023-07-31 14:42:53
 * @LastEditTime: 2023-08-03 14:36:59
 * @Description: file content
 */
/*
 * @lc app=leetcode.cn id=83 lang=typescript
 *
 * [83] 删除排序链表中的重复元素
 *
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-list/description/
 *
 * algorithms
 * Easy (53.08%)
 * Likes:    1018
 * Dislikes: 0
 * Total Accepted:    594.4K
 * Total Submissions: 1.1M
 * Testcase Example:  '[1,1,2]'
 *
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 * 
 * 
 * 
 * 示例 1：
 * 
 * 
 * 输入：head = [1,1,2]
 * 输出：[1,2]
 * 
 * 
 * 示例 2：
 * 
 * 
 * 输入：head = [1,1,2,3,3]
 * 输出：[1,2,3]
 * 
 * 
 * 
 * 
 * 提示：
 * 
 * 
 * 链表中节点数目在范围 [0, 300] 内
 * -100 <= Node.val <= 100
 * 题目数据保证链表已经按升序 排列
 * 
 * 
 */

// @lc code=start
/**
 *Definition for singly-linked list.
 */
  class ListNode {
      val: number
      next: ListNode | null
      constructor(val?: number, next?: ListNode | null) {
          this.val = (val===undefined ? 0 : val)
          this.next = (next===undefined ? null : next)
      }
  }

function deleteDuplicates(head: ListNode | null): ListNode | null {
    let s: Set<number> = new Set(), node: ListNode = head
    while (head!=null&&head.next != null) {
        if (!s.has(head.val)&&head.val!==head.next.val) {
            s.add(head.val)
            head=head.next
        } else {
            head.next = head.next.next
        }
    }
    return node
};
// @lc code=end

